﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;

namespace SyncWayBill.Model
{
    /// <summary>
    /// http请求类
    /// </summary>
    public class HttpHelpr
    {
        public static string Post<T>(string url, T par)
        {
            var client = new HttpClient();
            var parameter = Newtonsoft.Json.JsonConvert.SerializeObject(par);
            HttpContent httpContent = new StringContent(parameter);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            var response = client.PostAsync(url, httpContent).Result;

            var responeStr = response.Content.ReadAsStringAsync().Result;
            return responeStr;
        }
        public static Tout Post<Tin, Tout>(string url, Tin par)
        {
            var client = new HttpClient();
            var parameter = Newtonsoft.Json.JsonConvert.SerializeObject(par);
            HttpContent httpContent = new StringContent(parameter);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            var response = client.PostAsync(url, httpContent).Result;
            var responeStr = response.Content.ReadAsStringAsync().Result;
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Tout>(responeStr);
        }
        public static string Post(string url, string parameter)
        {
            var client = new HttpClient();
            HttpContent httpContent = new StringContent(parameter);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            Dictionary<string, string> dic = getYwHeader();
            foreach (var item in dic)
            {
                httpContent.Headers.Add(item.Key, item.Value);
            }
            var response = client.PostAsync(url, httpContent).Result;

            var responeStr = response.Content.ReadAsStringAsync().Result;
            return responeStr;
        }
        /// <summary>
        /// 获取运网header信息
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> getYwHeader()
        {
            var appKey = "c6652d55";
            var secretKey = "caad4e85832b3e976a";
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string timeStamp = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            string signature = GenerateMD5(appKey + secretKey + timeStamp);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("appKey", appKey);
            dic.Add("timeStamp", timeStamp);
            dic.Add("signature", signature);
            return dic;
        }
        /// <summary>
        /// MD5字符串加密
        /// </summary>
        /// <param name="txt"></param>
        /// <returns>加密后字符串</returns>
        public static string GenerateMD5(string txt)
        {
            using (MD5 mi = MD5.Create())
            {
                byte[] buffer = Encoding.Default.GetBytes(txt);
                //开始加密
                byte[] newBuffer = mi.ComputeHash(buffer);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < newBuffer.Length; i++)
                {
                    sb.Append(newBuffer[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }
    }
}
